package gnolls.service.impl;

import java.util.List;

import gnolls.dao.MessageDao;
import gnolls.domain.Message;
import gnolls.service.CommonService;
import gnolls.service.exceptions.ServiceException;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

public class CommonServiceImpl implements CommonService {

	private MessageDao messageDao;

	private static final Logger LOG = Logger.getLogger(CommonServiceImpl.class);

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public String testSmoke() {
		LOG.info("CommonServiceImpl.testSmoke()");
		return "test passed";
	}

	@Transactional
	public boolean sendMessage(String text) throws ServiceException {
		LOG.info("message: [" + text + "]");
		Message message = new Message();
		message.setText(text);
		Long id = messageDao.create(message);
		return (id == null) ? false : true;
	}
	@Override
	@Transactional
	public String getMessages() throws ServiceException {
		String result = "";
		List<Message> messages = messageDao.findAll();
		for(Message message : messages){
			result += message.getText();
		}
		return result;
	}

}
